<?php
$this->set_css($this->default_theme_path . '/flexigrid/css/flexigrid.css');
$this->set_js($this->default_javascript_path . '/' . grocery_CRUD::JQUERY);
$this->set_js($this->default_theme_path . '/flexigrid/js/cookies.js');
$this->set_js($this->default_theme_path . '/flexigrid/js/flexigrid.js');
$this->set_js($this->default_theme_path . '/flexigrid/js/jquery.form.js');
$this->set_js($this->default_javascript_path . '/jquery_plugins/jquery.numeric.min.js');
$this->set_js($this->default_theme_path . '/flexigrid/js/jquery.printElement.min.js');

/** Fancybox */
$this->set_css($this->default_css_path . '/jquery_plugins/fancybox/jquery.fancybox.css');
$this->set_js($this->default_javascript_path . '/jquery_plugins/jquery.fancybox.pack.js');
$this->set_js($this->default_javascript_path . '/jquery_plugins/jquery.easing-1.3.pack.js');

// RESPONSAVEL EM 
foreach ($list as $num_row => $row) {break;};
if (!empty($row->action_urls)){
    foreach ($row->action_urls as $action_unique_id => $action_url) {
                                    $action[] = $actions[$action_unique_id];
    }
}
?>
  
<script type='text/javascript'>
    var base_url = '<?php echo base_url(); ?>';

    var subject = '<?php echo $subject ?>';
    var ajax_list_info_url = '<?php echo $ajax_list_info_url ?>';
    var unique_hash = '<?php echo $unique_hash; ?>';

    var message_alert_delete = "<?php echo $this->l('alert_delete'); ?>";

</script>
<div id="hidden-operations"></div>
<div id='report-error' class='report-div error'></div>
<div id='report-success' class='report-div success report-list' <?php if ($success_message !== null) { ?>style="display:block"<?php } ?>><?php if ($success_message !== null) { ?>
        <p><?php echo $success_message; ?></p>
    <?php }
    ?></div>	
<div class="flexigrid" style='width: 100%;'>
    <div class="mDiv">
        <div class="ftitle">
            &nbsp;
        </div>
        <div title="<?php echo $this->l('minimize_maximize'); ?>" class="ptogtitle">
            <span></span>
        </div>
    </div>
    <div id='main-table-box'>

        <?php if (!$unset_add || !$unset_export || !$unset_print) { ?>
            <div class="tDiv">
                <?php if (!$unset_add) { ?>
                    <div class="tDiv2">
                        <a href='<?php echo $add_url ?>' title='<?php echo $this->l('list_add'); ?> <?php echo $subject ?>' class='add-anchor'>
                            <div class="fbutton">
                                <div>
                                    <span class="add"><?php echo $this->l('list_add'); ?> <?php echo $subject ?></span>
                                </div>
                            </div>
                        </a>
                        <div class="btnseparator">
                        </div>
                    </div>
                <?php } ?>
                <div class="tDiv3">
                    <?php if (!$unset_export) { ?>
                        <a class="export-anchor" data-url="<?php echo $export_url; ?>" target="_blank">
                            <div class="fbutton">
                                <div>
                                    <span class="export"><?php echo $this->l('list_export'); ?></span>
                                </div>
                            </div>
                        </a>
                        <div class="btnseparator"></div>
                    <?php } ?>
                    <?php if (!$unset_print) { ?>
                        <a class="print-anchor" data-url="<?php echo $print_url; ?>">
                            <div class="fbutton">
                                <div>
                                    <span class="print"><?php echo $this->l('list_print'); ?></span>
                                </div>
                            </div>
                        </a>
                        <div class="btnseparator"></div>
                    <?php } ?>						
                </div>
                <div class='clear'></div>
            </div>
        <?php } ?>

        <div id='ajax_list'>
            <?php echo $list_view ?>
        </div>
        <?php echo form_open($ajax_list_url, 'method="post" id="filtering_form" autocomplete = "off"'); ?>	
        <div class="sDiv" id='quickSearchBox'>
            <div class="sDiv2">
                <?php echo $this->l('list_search'); ?>: <input type="text" class="qsbsearch_fieldox" name="search_text" size="30" id='search_text'>
                <select name="search_field" id="search_field">
                    <option value=""><?php echo $this->l('list_search_all'); ?></option>
                    <?php foreach ($columns as $column) { ?>
                        <option value="<?php echo $column->field_name ?>"><?php echo $column->display_as ?>&nbsp;&nbsp;</option>
                    <?php } ?>
                </select>
                <input type="button" value="<?php echo $this->l('list_search'); ?>" id='crud_search'> 
            </div>
            <div class='search-div-clear-button'>
                <input type="button" value="<?php echo $this->l('list_clear_filtering'); ?>" id='search_clear'>
            </div>
        </div>
        <div class="pDiv">
            <div class="pDiv2">
                <?php if($multiselect == 1){
                        if (!empty($action)){
                            foreach ($action as $extras){
                                if($extras->label == 'multiselect'){
                                    $dbInfo = explode("|",base64_decode($extras->css_class));
                                }
                            }
                            
                            if ($dbInfo[2] != '0'){
                                $campos = explode("#",$dbInfo[2]);
                                $opcoes_extras = '';
                                foreach($campos as $campos_bd){
                                    $explode = explode(',',$campos_bd);
                                    $opcoes_extras .= "<option avisar='sim' value='".bin2hex('1,'.$explode[0].','.$dbInfo[1])."'>Ativar ".$explode[1]."</option>
                                                      <option avisar='sim' value='".bin2hex('0,'.$explode[0].','.$dbInfo[1])."'>Desativar  ".$explode[1]."</option>";
                                }
                            }
                        }
                        
                    ?>
                <div class="pGroup">
                    <select name="acao_multi_select" onchange="acao_select($(this),'<?php echo($dbInfo[4])?>')" id='acao_multi_select'>
                        <option avisar='nao' >Selecione</option>
                        
                        <?php if($dbInfo[3] == 1) { ?>
                            <?php echo ("<option avisar='nao' value='".bin2hex('1,showjustSelectedIDS,'.$dbInfo[1])."'>Mostrar apenas os marcados</option>"); ?>
                        <?php } ?>
                        
                        <?php echo ($dbInfo[0] > 0 ? "<option avisar='sim' value='".bin2hex('1,delete,'.$dbInfo[1])."'>Deletar Marcados</option>" : ''); ?>
                        
                        <?php echo (isset($opcoes_extras)?$opcoes_extras:''); ?>
                    </select>
                </div>
                 <div class="btnseparator"></div>
                <?php } ?>
                <div class="pGroup">
                    <div class="pSearch pButton" id='quickSearchButton' title="<?php echo $this->l('list_search'); ?>">
                        <span></span>
                    </div>
                </div>
                <div class="btnseparator">
                </div>
                <div class="pGroup">
                    <select name="per_page" id='per_page'>
                        <?php foreach ($paging_options as $option) { ?>
                            <option value="<?php echo $option; ?>" <?php if ($option == $default_per_page) { ?>selected="selected"<?php } ?>><?php echo $option; ?>&nbsp;&nbsp;</option>
                        <?php } ?>
                    </select>
                    <input type='hidden' name='order_by[0]' id='hidden-sorting' value='<?php if (!empty($order_by[0])) { ?><?php echo $order_by[0] ?><?php } ?>' />
                    <input type='hidden' name='order_by[1]' id='hidden-ordering'  value='<?php if (!empty($order_by[1])) { ?><?php echo $order_by[1] ?><?php } ?>'/>
                </div>
                <div class="btnseparator">
                </div>
                <div class="pGroup">
                    <div class="pFirst pButton first-button">
                        <span></span>
                    </div>
                    <div class="pPrev pButton prev-button">
                        <span></span>
                    </div>
                </div>
                <div class="btnseparator">
                </div>
                <div class="pGroup">
                    <span class="pcontrol"><?php echo $this->l('list_page'); ?> <input name='page' type="text" value="1" size="4" id='crud_page'> 
                        <?php echo $this->l('list_paging_of'); ?> 
                        <span id='last-page-number'><?php echo ceil($total_results / $default_per_page) ?></span></span>
                </div>
                <div class="btnseparator">
                </div>
                <div class="pGroup">
                    <div class="pNext pButton next-button" >
                        <span></span>
                    </div>
                    <div class="pLast pButton last-button">
                        <span></span>
                    </div>
                </div>
                <div class="btnseparator">
                </div>
                <div class="pGroup">
                    <div class="pReload pButton" id='ajax_refresh_and_loading'>
                        <span></span>
                    </div>
                </div>
                <div class="btnseparator">
                </div>
                <div class="pGroup">
                    <span class="pPageStat">
                        <?php $paging_starts_from = "<span id='page-starts-from'>1</span>"; ?>
                        <?php $paging_ends_to = "<span id='page-ends-to'>" . ($total_results < $default_per_page ? $total_results : $default_per_page) . "</span>"; ?>
                        <?php $paging_total_results = "<span id='total_items'>$total_results</span>" ?>
                        <?php
                        echo str_replace(array('{start}', '{end}', '{results}'), array($paging_starts_from, $paging_ends_to, $paging_total_results), $this->l('list_displaying')
                        );
                        ?>   					
                    </span>
                </div>
            </div>
            <div style="clear: both;">
            </div>
        </div>
        <?php echo form_close(); ?>
    </div>
</div>



<script> 
    
<?php if (!$unset_edit) { ?>
     
     // no double click da row da grid ele chama esta funcion
     function editMe(url){
        window.location.href = url;
    }
     
<?php } ?>

    
<?php if ($detailbox != '') { ?>
    
    // se o usuario chamou no controller add_detail_box, entao precisamos deta function
    function detailBox(url,w,h,Windowtitle){
        $.fancybox({
            'padding': 20,
            'autoScale':true,
            'overlayShow': true,
            'transitionIn': 'elastic',
            'transitionOut'	: 'elastic',
            'width': w,
            'height': h,
            'href' : url,
            'title':Windowtitle,
            'titleShow':true,
            'titlePosition':'inside',            
            'type': 'iframe'       
        }); 
    }
<?php } 


// so declara este JS se ha algum switcher definido no grocery
if($switcher) { 
?>    

    // se ha algum campo do tipo switcher
    function switcher(dbinfo, field_name, row_id, true_img, false_img, time_img, urlBoolean){
        
        var fig_original = $('#switcher-'+field_name+'-'+row_id).attr('src');

        // say to user that something is happening.. 
        $('#switcher-'+field_name+'-'+row_id).attr('src',time_img);

        var url = urlBoolean+dbinfo+"/"+row_id;

         $.get(url,function(data,status){

             // se a imagem original era false, agora eh true
             if(fig_original == false_img){

                 $('#switcher-'+field_name+'-'+row_id).attr('src',true_img);

             } else {

                 $('#switcher-'+field_name+'-'+row_id).attr('src',false_img);
             }    

        });

    }

<?php } 
if($list_edit_order) { 
?>    
    
    // se ha algum campo do tipo list_edit_order
    
    $(document).ready(function(){
      $("#ordem").bind("keyup blur focus", function(e) {
            var expre = /[^\d]/g;
            $(this).val($(this).val().replace(expre,''));
       });
    });
    
    function alterar_ordem(field_name, id, ordem, tabela, actionURL){
        if($.trim(ordem) == ''){
            ordem = '_';
        }
        
        var url = actionURL+field_name+"/"+id+"/"+ordem+"/"+tabela;
        
        $.post(url, {})
            .done(function(data){
                if(data != ''){
                    $("#ordem"+id).val(data);
                    alert('Preenchimento obrigatório!\nVoltamos o valor anterior!');
                }
            });


    }



<?php } 



if($multiselect == 1){ ?>
    
    function CheckAll(){
        if($('.checkall').is(':checked') == false){
            $('table').find('[name=custom_select]').removeAttr('checked');
        }else{
            $('table').find('[name=custom_select]').attr('checked','true');
        }
    }
    
    function acao_select(tipo_acao, actionURL){
        
        var dados = ''; 
        var checados = '';
        
        $('td input[type=checkbox]').each(function() { 
		if (this.checked) {
                    checados = true;
                    dados += this.value + '_';
		}
	});
    
        if(checados == true){
        
            //  se eh uma operação perigosa como delete, update, marca  na option
            // com "avisar" valor "sim" para que antes de fazer peça autorização ao usuario

            if(tipo_acao.find('option:selected').attr('avisar') == 'sim'){

                var txtmessage = "Executar a ação: "+tipo_acao.find('option:selected').text()+" ?";

                if(!confirm(txtmessage)){
                    return false; // se nao conformar, cai fora da function, nao faz nada
                }
            }        
        
        
             $.post(actionURL+tipo_acao.val()+'/'+dados)
                .done(function(data){
                    $('html, body').animate({
                        scrollTop: $(".flexigrid").offset().top
                    }, 1000);
                    location.reload();
                })
                .fail(function(data){
                    $("#acao_multi_select").val($("#acao_multi_select option:first").val());
                    alert('Ocorreu um problema na hora da atualização!\nTente novamente');
                });
        }else{
            $("#acao_multi_select").val($("#acao_multi_select option:first").val());
            alert('É necessário selecionar pelo menos um campo!');
        }
        
    }

<?php } ?>
</script>